package com.winsdom.controller;

import com.winsdom.anno.Authentication;
import com.winsdom.annotation.OperationLog;
import com.winsdom.dto.AccountInformationDTO;
import com.winsdom.dto.CategoryTreeNodes;
import com.winsdom.dto.JurisdictionDTO;
import com.winsdom.dto.SysUserDTO;
import com.winsdom.result.PageResult;
import com.winsdom.result.Result;
import com.winsdom.service.ICategoryService;
import com.winsdom.service.ISysRoleService;
import com.winsdom.service.ISysUserService;
import com.winsdom.service.IUserProjectsService;
import com.winsdom.vo.CodeRuleVO;
import com.winsdom.vo.RoleInformationVO;
import com.winsdom.vo.UserOrgInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.winsdom.enums.AuthenticationType.ACCOUNT_MANAGEMENT;


@RestController
@RequestMapping("/sys/user")
@Api(tags = "账号管理")
@Slf4j
@Validated
@Authentication(value = ACCOUNT_MANAGEMENT)
public class SysUserController {
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ISysRoleService iSysRoleService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IUserProjectsService userProjectsService;

    /**
     * 查询账号
     * @param accountInformationDTO
     * @return
     */
    @PostMapping("/account")
    @ApiOperation("查询账号")
    @OperationLog(description = "查询账号")
    public Result getAllAccount(@RequestBody AccountInformationDTO accountInformationDTO){
        log.info("查询条件为{} 的账户", accountInformationDTO);
        PageResult allAccount = iSysUserService.getAllAccount(accountInformationDTO);
        return Result.success(allAccount);
    }

    /**
     * 查询角色
     * @return
     */
    @PostMapping("/role")
    @ApiOperation("查询角色")
    @OperationLog(description = "查询角色")
    public Result getAllRole(){
        List<RoleInformationVO> roleInformationVO=iSysRoleService.getAllRole();
        return Result.success(roleInformationVO);
    }

    /**
     * 新增用户
     * @param sysUserDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增用户")
    @OperationLog(description = "新增用户")
    public Result add(@Valid @RequestBody SysUserDTO sysUserDTO){
        log.info("添加用户信息:{}",sysUserDTO);
        iSysUserService.add(sysUserDTO);
        return Result.success("新增成功");
    }

    /**
     * 修改用户
     * @param sysUserDTO 用户信息
     * @return 操作结果
     */
    @PostMapping("/update")
    @ApiOperation("修改用户")
    @OperationLog(description = "修改用户")
    public Result update(@Valid @RequestBody SysUserDTO sysUserDTO) {
        log.info("修改用户信息:{}", sysUserDTO);
        iSysUserService.update(sysUserDTO);
        return Result.success("修改成功");
    }

    /**
     * 获取当前用户可管理的组织树（包含子组织）
     * @return
     */
    @GetMapping("/organizations/tree")
    @ApiOperation("组织树（包含子组织）")
    @OperationLog(description = "获取当前用户可管理的组织树")
    public Result<CategoryTreeNodes> getOrganizationTree() {
        CategoryTreeNodes organizationTree = iSysUserService.getCurrentUserOrganizationTree();
        return Result.success(organizationTree);
    }

    /**
     * 重置密码为手机号后六位
     * @param id 用户ID
     * @return 操作结果
     */
    @GetMapping("/resetPassword")
    @ApiOperation("重置密码")
    @OperationLog(description = "重置密码为手机号后六位")
    public Result resetPassword(@RequestParam Long id) {
        log.info("重置用户ID为 {} 的密码", id);
        iSysUserService.resetPassword(id);
        return Result.success("密码已重置为手机号后六位");
    }

    /**
     * 获取编码规则
     * @return
     */
    @GetMapping("/codeRule")
    @ApiOperation("获取编码规则")
    @OperationLog(description = "获取编码规则")
    public Result<CodeRuleVO> getCodeRule() {
        return Result.success(iSysUserService.getCodeRule());
    }

    /**
     *根据用户名查询用户信息及组织树
     * @param username 用户名
     * @return
     */
    @GetMapping("/userOrgInfo")
    @ApiOperation(value = "根据用户名查询用户信息及组织树")
    @OperationLog(description = "根据用户名查询用户信息及组织树")
    public Result<UserOrgInfoVO> getUserOrgInfo(
            @RequestParam
            @ApiParam(value = "要查询的用户名", required = true, example = "CQ0001") String username) {

        log.info("开始查询用户信息及组织树，用户名: {}", username);
        UserOrgInfoVO userOrgInfo = categoryService.getUserOrgInfo(username);

        log.info("成功查询到用户信息及组织树，用户名: {}", username);
        return Result.success(userOrgInfo);
    }

    /**
     * 提交管辖项目
     * @param jurisdictionDTO 用户管辖信息
     * @return
     */
    @PostMapping("/saveOrg")
    @ApiOperation(value = "提交管辖项目")
    @OperationLog(description = "提交管辖项目树")
    public Result<String> saveNodes(
            @RequestBody @Valid
            @ApiParam(value = "包含用户名和节点ID列表的请求体", required = true) JurisdictionDTO jurisdictionDTO) {

        log.info("开始插入用户管辖项目表: {}", jurisdictionDTO);
        userProjectsService.saveUserProjects(jurisdictionDTO);

        log.info("插入用户管辖项目表成功！");
        return Result.success("添加成功！");
    }
}
